<template>
  <div class>
    <p style="font-size: 30px;font-weight: 700;margin-top:0">学校客户自动分配策略组</p>
    <div class="group-box">
      <el-row>
        <el-col :span="12">
           <span style=" font-size: 24px;font-weight: 700;">是否允许自动分配（资源划分中的资源）：</span>
        </el-col>
        <el-col :span="12">
           <el-radio-group  v-model="auto">
            <el-radio label="1" size="medium">是</el-radio>
            <el-radio label="2" size="medium">否</el-radio>
          </el-radio-group>
        </el-col>
      </el-row>
      <div v-if="auto==1">
      <el-row>
        <el-col :span="12">
          <span style=" font-size: 24px;font-weight: 700;">自动分配方式：</span>
        </el-col>
        <el-col :span="12">
           <el-radio-group  v-model="radio">
                <el-radio label="2" size="medium">按部门分配</el-radio>
                <el-radio label="1" size="medium">按个人分配</el-radio>
            </el-radio-group>
        </el-col>
      </el-row>
    <DepartmentTable
      :datas="deptLevelData2"
      @inputChange="inputChange"
      @input2Change="input2Change"
      v-if="radio == 2"
    ></DepartmentTable>
    <PersonalTable  :datas="date"  :deptUserName="deptUserName2"  v-if="radio == 1"></PersonalTable>
    </div>
    </div>
    
    <div>
      <KtButton
        :label="$t('action.close')"
        perms="true"
        type="primary"
        size="medium"
        style="margin-right: 2%;"
        @click="closeSelectedTag"
      ></KtButton>
      <KtButton
        :label="$t('action.save')"
        perms="true"
        type="primary"
        size="medium"
        v-if="radio == 2&auto == 1"
        style="margin-top: 2%; "
        @click="saveDepart"
      ></KtButton>
      <KtButton
        :label="$t('action.save')"
        perms="true"
        type="primary"
        size="medium"
        v-if="radio == 1&auto == 1"
        style="margin-top: 2%; "
        @click="savePerson"
      ></KtButton>
      <KtButton
        :label="$t('action.save')"
        perms="true"
        type="primary"
        size="medium"
        v-if="auto == 2"
        style="margin-top: 2%; "
        @click="saveAuto"
      ></KtButton>
    </div>
  </div>
</template>

<script>
import PersonalTable from "@/views/Core/PersonalTable";
import DepartmentTable from "@/views/Core/DepartmentTable";
import KtButton from "@/views/Core/KtButton";
export default {
  name: "School",
  components: {
    PersonalTable,
    DepartmentTable,
    KtButton
  },
  data() {
    return {
      radio: "",
      auto: "",
      date: [],
      num:[],//部门分配数量
      num_p:[],//个人分配
      tableData: [],
      listCustomerAutomaticAssignStrategy: [],
      strategyConfigQueryStrategy_depart:[],
      deptLevelData:[],
      deptLevelData2:[],
      deptUserName:[],
      deptUserName2:[]
    };
  },
  methods: {
    // 请求三级部门
    get_deptLevel3() {
      this.tableData=JSON.parse(localStorage.getItem('deptLevel3School'))
      this.deptLevelData=JSON.parse(localStorage.getItem('deptLevel3School'))
      this.date=JSON.parse(localStorage.getItem('deptLevel3School'))
       this.date.forEach(item=>{
          item.text=item.deptName
          item.value=item.deptName
        })
      // this.$api.dropdown.deptLevel3School().then(res => {
      //   this.tableData=res.departmentInfoList
      //   this.deptLevelData=res.departmentInfoList
      //   this.date=res.departmentInfoList
      //   this.date.forEach(item=>{
      //     item.text=item.deptName
      //     item.value=item.deptName
      //   })
      // });
      this.get_strategyConfigQueryStrategy_depart();
    },
    // 按照部门查询分配策略
    get_strategyConfigQueryStrategy_depart(){
       this.$api.student.strategyConfigQueryStrategy({
        customerType:2,//1 学生 3 学酷 4 网校  belongToType=2部门1个人
        belongToType:2
      }).then(res=>{
        this.num=res.listCustomerAutomaticAssignStrategy
        this.deptLevelData.forEach((item_dept,index_dept)=>{
          this.num.forEach((item_num,index_num)=>{
            if(item_dept.id==item_num.toId){
              item_dept.priority=item_num.priority
              item_dept.num=item_num.num
            }
          })
        })
        this.deptLevelData2 = this.deptLevelData;
      })
    },
    // 按照个人查询分配策略
    get_strategyConfigQueryStrategy_person(){
       this.$api.student.strategyConfigQueryStrategy({
        customerType:2,
        belongToType:1
      }).then(res=>{
        this.num_p=res.listCustomerAutomaticAssignStrategy
        this.deptUserName.forEach((item_dept,index_dept)=>{
          this.num_p.forEach((item_num,index_num)=>{
            if(item_dept.id==item_num.toId){
              item_dept.priority=item_num.priority
              item_dept.num=item_num.num
            }
          })
        })
        this.deptUserName2=this.deptUserName
      })
    },
    // 获取是否允许自动分配
    get_allow_student_autoware_assign(){
      this.$api.student.strategyConfigQueryConfig({
        names:'ALLOW_SCHOOL_AUTOWARE_ASSIGN',
        type:2
      }).then(res=>{
        this.auto=res.customerAssignConfig[0].value
      })
    },
    // 自动分配方式
    get_student_aotuware_assign_type(){
      this.$api.student.strategyConfigQueryConfig({
        names:'SCHOOL_AUTOWARE_ASSIGN_TYPE',
        type:2
      }).then(res=>{
        this.radio=res.customerAssignConfig[0].value
      })
    },
    // 优先级
    inputChange(params) {
      this.listCustomerAutomaticAssignStrategy = Object.assign({}, params.row);
    },
    // 分配条数
    input2Change(params) {
      this.listCustomerAutomaticAssignStrategy = Object.assign({}, params.row);
    },
    saveDepart(){
       this.$confirm("确定提交信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(()=>{
        let aa=[]
        let bb=[]
        let customerAssignConfigList=Object.assign({}, this.auto);
        let customerAssignConfigList2=Object.assign({}, this.radio);
        aa.push(customerAssignConfigList)
        bb.push(customerAssignConfigList2)
        aa.forEach(item=>{
          item.value=this.auto
          item.name='ALLOW_SCHOOL_AUTOWARE_ASSIGN'
        })
        bb.forEach(item=>{
          item.value=this.radio
          item.name='SCHOOL_AUTOWARE_ASSIGN_TYPE'
        })
        this.deptLevelData.forEach(item=>{
          item.toId=item.id
        })
        this.deptLevelData.forEach(item=>{
          delete item.id
          delete item.children
          delete item.createDate
          delete item.creater
          delete item.deptLevel
          delete item.deptName
          delete item.isDelete
          delete item.parentId
          delete item.deptType
          delete item.text
          delete item.value
          if(!item.priority){
            item.priority='0'
          }
          if(!item.num){
            item.num='0'
          }
        })
        this.$api.student.strategyConfigSetStrategy({
             listCustomerAutomaticAssignStrategy:this.deptLevelData,
             customerType:2,
             belongToType:2
            //  customerType=1 学生客户 belongToType=2部门1个人
           }).then(res=>{
             if(res.result==2){
              this.$message({message: res.msg,type: 'error'})
            }else{
               this.$message({message: res.msg,type: 'success'})
            }
        })
        this.$api.student.strategyConfigSetConfig({
           customerAssignConfigList:aa,
           type:2
        }).then(res=>{

        })
        this.$api.student.strategyConfigSetConfig({
           customerAssignConfigList:bb,
           type:2
        }).then(res=>{

        })
        this.get_deptLevel3()
      })
        
    },
    savePerson(){
       this.$confirm("确定提交信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(()=>{
        let aa=[]
      let bb=[]
      let customerAssignConfigList=Object.assign({}, this.auto);
      let customerAssignConfigList2=Object.assign({}, this.radio);
      aa.push(customerAssignConfigList)
      bb.push(customerAssignConfigList2)
      aa.forEach(item=>{
        item.value=this.auto
        item.name='ALLOW_SCHOOL_AUTOWARE_ASSIGN'
      })
      this.tableData.forEach(item=>{
        item.toId=item.id
      })
      bb.forEach(item=>{
        item.value=this.radio
        item.name='SCHOOL_AUTOWARE_ASSIGN_TYPE'
      })
      this.$api.student.strategyConfigSetConfig({
           customerAssignConfigList:aa,
           type:2
        }).then(res=>{
          
        })
        this.$api.student.strategyConfigSetConfig({
           customerAssignConfigList:bb,
           type:2
        }).then(res=>{
          
        })
      this.deptUserName.forEach(item=>{
          if(!item.priority){
            item.priority='0'
          }
          if(!item.num){
            item.num='0'
          }
      })
      this.$api.student.strategyConfigSetStrategy({
          listCustomerAutomaticAssignStrategy:this.deptUserName,
          customerType:2,
          belongToType:1
        }).then(res=>{
          if(res.result==2){
          this.$message({message: res.msg,type: 'error'})
        }else{
            this.$message({message: res.msg,type: 'success'})
        }
      })
      this.get_deptLevel3()
      })
      
    },
    // 获取部门人员
    get_person(){
      this.$api.person.findPageSchool({accountStatus:1,isControlResource: 1}).then(res => {
         this.deptUserName=res.userInfoList
          this.deptUserName.forEach(item=>{
            item.toId=item.id
          })
        this.get_strategyConfigQueryStrategy_person()
      })
    },
    saveAuto() {
       this.$confirm("确定提交信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(()=>{
        let aa = [];
      let customerAssignConfigList = Object.assign({}, this.auto);
      aa.push(customerAssignConfigList);
      aa.forEach(item => {
        item.value = this.auto;
        item.name = "ALLOW_SCHOOL_AUTOWARE_ASSIGN";
      });
      this.$api.student
        .strategyConfigSetConfig({
          customerAssignConfigList: aa,
          type:2
        })
        .then(res => {
          if (res.result == 2) {
            this.$message({ message: res.msg, type: "error" });
          } else {
            this.$message({ message: "操作成功！", type: "success" });
          }
        });
      this.get_deptLevel3();
      })  
      
    },
    isActive(route) {
      return route.path === this.$route.path;
    },
    closeSelectedTag(view) {
      this.$store.dispatch("delVisitedViews", this.$route).then(views => {
        if (this.isActive(this.$route)) {
          const latestView = views.slice(-1)[0];
          if (latestView) {
            this.$router.push(latestView);
          } else {
            this.$router.push("/");
          }
        }
      });
    }
  },
  mounted() {
    this.get_deptLevel3();
    this.get_person();
    this.get_allow_student_autoware_assign();
    this.get_student_aotuware_assign_type();
  }
};
</script>

<style lang="scss">
.el-table {
  display: block;
}
.has-gutter tr th {
  text-align: center;
}
.el-table__row td > .cell {
  text-align: center;
}
.group-box {
  width: 80%;
  margin: 0 auto;
  .el-row {
    margin: 4% 0;
    .el-col-12 {
      text-align: left;
    }
  }
}
</style>
<style lang='scss' scoped>
.el-row .el-col{
  margin-left: 0;
}
.container .el-radio-group .el-radio:first-child{
  display: inline-block;
}
</style>